package com.gskorupa.univermon.versatile;

import com.google.gson.Gson;
import com.gskorupa.univermon.auth.RegisteredClients;
import com.gskorupa.univermon.query.Parser;
import com.gskorupa.univermon.storage.Persistent;
import java.io.IOException;
//import java.util.Date;
import javax.servlet.http.*;
//import com.google.appengine.api.users.User;
//import com.google.appengine.api.users.UserService;
//import com.google.appengine.api.users.UserServiceFactory;

public class Versatile extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        //
        RegisteredClients clients = RegisteredClients.getInstance();
        String uid = "" + req.getParameter("uid");
        String addr = req.getRemoteAddr();
        String query = req.getParameter("query");
        String limit=req.getParameter("limit");
        String fifo=req.getParameter("fifo");
        boolean registered = false;
        String userUID = null;
        String deviceName = null;

        // probably it's too simple
        // uidd equest parameter has pattern uid@deviceName ( eg 99999999999999999999999999999999@device1 )
        // where uid is a MD5 digest of the user e-mail address 
        if (!(uid.equals("null") || uid.isEmpty())) {
            userUID = getClientUID(uid);
            deviceName = getClientDevice(uid);
            registered = clients.isRegistered(userUID, deviceName) == RegisteredClients.OK;
        }

        if (!registered) {
            resp.sendRedirect("/");
            return;
        }

        Gson gson = new Gson();
        ReportEntity ent = new ReportEntity();
        resp.setContentType("application/json");
        if (query == null) {
            ent.setTimestamp();
            //store entity
            ent.setClientIP(addr);
            ent.setUserUID(userUID);
            ent.setClientName(deviceName);
            ent.setReportType(req.getParameter("type"));
            ent.setParam1(req.getParameter("param1"));
            ent.setParam2(req.getParameter("param2"));
            ent.setValue(req.getParameter("value"));
            Persistent.getInstance().addEntry(ent);
            resp.getWriter().println(gson.toJson("Hello " + uid));
        } else if (query.equalsIgnoreCase("error")) {
            resp.sendError(501, "forced");
        } else {
            Report report = new Report();
            report.setUser(userUID);
            report.setQuery(Parser.getInstance().parse(query));
            report.setLimit(limit);
            report.setFifo(fifo);

            //request DB
            //InMemory storage=InMemory.getInstance();
            Persistent storage=Persistent.getInstance();
            report=storage.getReport(report);

            //send response
            resp.getWriter().println(gson.toJson(report));
        }
    }

    String getClientUID(String uidParam) {
        int pos = uidParam.indexOf("@");
        String result = uidParam;
        if (pos > 0) {
            result = uidParam.substring(0, pos);
        }
        return result;
    }

    String getClientDevice(String uidParam) {
        int pos = uidParam.indexOf("@");
        String result = "";
        if (pos > 0) {
            result = uidParam.substring(pos + 1);
        }
        return result;
    }
}
